package org.akomantoso.core.data;

import java.io.IOException;

import org.akomantoso.core.data.attribute.IdReq;
import org.akomantoso.core.data.group.DocumentType;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.group.DocumentTypeGroup;

/**
 * <pre>
 * <xsd:element name="component">
 * 		<xsd:complexType>
 * 			<xsd:sequence>
 * 				<xsd:group ref="documentType"/>
 *			</xsd:sequence>
 * 			<xsd:attributeGroup ref="idreq"/>
 * 		</xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class Component implements IdReq , Externalizable{

	public static final String ELEMENT = "component";
	
	private DocumentType _documentType;
	
	private String _id;
	private String _evolvingId;
	
	/**
	 * @return the documentType
	 */
	public DocumentType getDocumentType() {
		return _documentType;
	}

	/**
	 * @param documentType the documentType to set
	 */
	public void setDocumentType(DocumentType documentType) {
		_documentType = documentType;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#getId()
	 */
	public String getId() {
		return _id;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#setId(java.lang.String)
	 */
	public void setId(String id) {
		_id = id;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#getEvolvingId()
	 */
	public String getEvolvingId() {
		return _evolvingId;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.IdReq#setEvolvingId(java.lang.String)
	 */
	public void setEvolvingId(String evolvingId) {
		_evolvingId = evolvingId;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeIdReq(out, this);
		_documentType.writeExternal(out);
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		_id = in.readAttribute(IdReq.ATTRIBUTE_ID);
		_evolvingId = in.readAttribute(IdReq.ATTRIBUTE_EVOLVING_ID);
		in.readNextStartElement();
		_documentType = DocumentTypeGroup.instantiate(in.getLocalName());
		_documentType.readExternal(in);
	}
	
}